﻿package com.quickdev.intelligenthome.utils
{
	import flash.utils.ByteArray;

	public class CRC32 extends CRC
	{
		public function CRC32()
		{
			super();
		}
		
		override protected function initCRCTable():void {
			trace("in CRC32.initCRCTable");
			var table :Array=new Array(256);  
			for (var i:int=0; i < 256; i++) {  
				var _crc:uint=i;  
				for (var j:int=0; j < 8; j++) {  
					_crc=(_crc & 1)?(_crc >>> 1) ^ 0xEDB88320:(_crc >>> 1);  
				}  
				table[i]=_crc;  
			}  
			
			crcTable = table;
		}
		
		override public function update(buffer :ByteArray, offset :uint = 0, length :int = -1):void {
			if( length == -1 ) {
				length = buffer.length;
			}
  
			var _crc :uint = ~crc;  
			for (var i:int=offset; i < length; i++) {  
				_crc = crcTable[(_crc ^ buffer[i]) & 0xFF] ^ (_crc >>> 8);  
			}  
			crc = ~_crc;  
		}
	}
}